using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Linq;
using XenoGears;
using XenoGears.Functional;
using XenoGears.Streams;

namespace Prjinit.Helpers
{
    [DebuggerNonUserCode]
    internal static class ProjectFiles
    {
        public static ReadOnlyCollection<String> EmbeddedResources
        {
            get
            {
                var asm = MethodInfo.GetCurrentMethod().DeclaringType.Assembly;
                var f_pfiles = asm.GetName().Name + ".Properties.ProjectFiles.xml";
                String s_pfiles; using (var s = asm.GetManifestResourceStream(f_pfiles)) { s_pfiles = s.DumpToString(); }
                var xml = XDocument.Parse(s_pfiles);

                var nsmgr = new XmlNamespaceManager(new NameTable());
                nsmgr.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");
                var x_ers = xml.XPathSelectElements("//msbuild:EmbeddedResource", nsmgr);
                var names = x_ers.Select(xel =>
                {
                    var x_include = xel.Attribute("Include");
                    var name = x_include == null ? null : x_include.Value;
                    return name == null ? null : name.Replace("%24", "$").Replace("%25", "%");
                }).ToArray();

                return names.ToReadOnly();
            }
        }
    }
}